﻿using System.Collections.Generic;
using System.Windows;
using Microsoft.Win32;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Packaging;

namespace OpenXMLValidator
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnOpenFileClick(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter = "Excel documents (.xlsx)|*.xlsx|Word documents (.docx)|*.docx|Power point documents (.pptx)|*.pptx"
            };

            var result = openFileDialog.ShowDialog();

            if (result == true)
                DataContext = GetErrors(openFileDialog.FileName);
        }

        private static IEnumerable<ValidationErrorInfo> GetErrors(string fileName)
        {
            var ext = fileName.Split('.')[1];
            var openXmlValidator = new OpenXmlValidator();
            switch (ext)
            {
                case "xlsx":
                    return openXmlValidator.Validate(SpreadsheetDocument.Open(fileName, true));
                case "docx":
                    return openXmlValidator.Validate(WordprocessingDocument.Open(fileName, true));
                case "pptx":
                    return openXmlValidator.Validate(PresentationDocument.Open(fileName, true));
                default:
                    return null;
            }
        }
    }
}
